package com.qiantu.confignetwork.hiflying.aplink.v1;

import android.util.Base64;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

public class ApLinkServFind {
    // 服务器参数，aesKey: 服务器通讯时AES加密密码; strUrl: 服务器路径;///////
    public static final String aesKey= "`aPliNkseRViCe$'";
//    public static final String strUrl= "http://101.200.39.154:8082";
    public static final String strUrl= "https://wlandevicefind.lingluzn.com"; //"http://wlandevicefind.lingluzn.com:8082"
    ///////////////////////////////////////////////////////////////////////
    private static final String paraKey= "aplink";

    public static String decrypt(String str) {
        byte[] b= Base64.decode(str, Base64.DEFAULT);
        if (b==null) {
            return null;
        }
        b= AESCrypt.HFDecrypt(b);
        if (b==null) {
            return null;
        }
        b= AESCrypt.unPadding(b);
        try {
            return new String(b, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String encrypt(String str) {
        byte[] b= b= str.getBytes();
        if (b==null) {
            return null;
        }
        b= AESCrypt.HFEncrypt(b);
        if (b==null) {
            return null;
        }
        return Base64.encodeToString(b, Base64.DEFAULT);
    }

    public static String packHttpGetReq(String MAC, String ServKey, String Time){
        if (MAC==null || ServKey==null || Time==null){
            return null;
        }
        String para= "MAC=%MAC%&ServKey=%ServKey%&Time=%Time%".replace("%MAC%", MAC).replace("%ServKey%", ServKey).replace("%Time%", Time);
        return encrypt(para);
    }

    public static String httpGet(String para){
        BufferedReader in = null;
        try {
            String urlPara = strUrl+"?"+paraKey+"="+ URLEncoder.encode(para, "utf-8");

            Log.i("httpGet","urlPara="+urlPara);
            URL url = new URL(urlPara);
            URLConnection connection = url.openConnection();
            connection.setRequestProperty("accept", "text/plain;charset=utf-8");
//            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            connection.connect();
            in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
            StringBuffer buffer = new StringBuffer();
            String line = "";
            while ((line = in.readLine()) != null) {
                buffer.append(line);
            }
            in.close();
            String recv= buffer.toString();
            if (recv!=null && !recv.equals("")){
                return decrypt(recv);
            }
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}
